<?php

/* 
 * 财务报表
 */

namespace Admin\Controller;

class FinancialController extends ComController
{
    public function index(){
       $prefix = C('DB_PREFIX');
       $uid = $_SESSION['uid'];
       $p= intval($_GET['p'])?$_GET['p']:1;
       $dateS = I('get.dateS')?I('get.dateS'):date('Y-m-d');
	   $dateE = I('get.dateE')?I('get.dateE'):date('Y-m-d',strtotime('+1 month'));
	   //1正常   2打印
	   $type = (int)I('get.type')?I('get.type'):1;
	   //支付类型  0全部（默认）1线上 2线下
	   $payType =(int)I('get.pay_type')?I('get.pay_type'):0;
       $pagesize = 15;#每页数量
       $offset = $pagesize * ($p - 1);//计算记录偏移量
       $level = $this->getType($uid);//获取当前登录的账户 是平台，还是幼儿园
     
       if($level == 1){
       	//幼儿园登录
       	$where = array('is_pay'=>1,"{$prefix}baoming.sc_id"=>$uid,"{$prefix}baoming.add_time"=>array('between',array($dateS,$dateE)));
       	if($payType){
       		$where['pay_type']=$payType;
       	}
           $allmoney = $this->getAllYeyRecord($dateS,$dateE,$payType);//获取幼儿园总收入金额
           if($type == 2){
           	$data = M('baoming')
           	->field(" {$prefix}baoming.*,m.uid,u.name as u_name,m.name as m_name,b.body_name as b_name,c.name as c_name ")
           	->join(" {$prefix}user as u on u.id={$prefix}baoming.user_id ","LEFT")
           	->join(" {$prefix}member as m on m.uid={$prefix}baoming.sc_id ","LEFT")
           	->join(" {$prefix}body as b on b.id={$prefix}baoming.body_id ","LEFT")
           	->join(" {$prefix}class as c on c.id={$prefix}baoming.class_id ","LEFT")
           	->where($where)
           	->order("{$prefix}baoming.add_time desc")
           	->select();
           }else{
	           	$data = M('baoming')
	           	->field(" {$prefix}baoming.*,m.uid,u.name as u_name,m.name as m_name,b.body_name as b_name,c.name as c_name ")
	           	->join(" {$prefix}user as u on u.id={$prefix}baoming.user_id ","LEFT")
	           	->join(" {$prefix}member as m on m.uid={$prefix}baoming.sc_id ","LEFT")
	           	->join(" {$prefix}body as b on b.id={$prefix}baoming.body_id ","LEFT")
	           	->join(" {$prefix}class as c on c.id={$prefix}baoming.class_id ","LEFT")
	           	->where($where)
	           	->limit($offset . ',' . $pagesize)
	           	->order("{$prefix}baoming.add_time desc")
	           	->select();
	           	
           }
            $count = M('baoming')
            		->field('sc_id')
                    ->where($where)
                    ->count();
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
            $this->assign('allmoney',$allmoney);
            $this->assign('name_type','幼儿园');
            $this->assign('page',$page);
            $this->assign('data',$data);
            $this->assign('dateS',$dateS);
		    $this->assign('dateE',$dateE);
		    if($type == 2){
		    	$this->display('index_1_pri');
		    }else{
           		$this->display('index_1');
		    }
       }else if($level == 0){
           //平台登录
           $allmoney = $this->getAllRecord($dateS,$dateE);//获取平台总收入金额
           $yeymoney = $this->getYeyRecord($offset,$pagesize,$dateS,$dateE,$type);//获取幼儿园收入金额
           $count = M('baoming')
           		->field('sc_id')
           		->where(array('pay_type'=>1,'add_time'=>array('between',array($dateS,$dateE))))
           		->group('sc_id')
                ->select();
           $count=count($count);
           $page = new \Think\Page($count, $pagesize);
           $page = $page->show();
           $this->assign('page',$page);
           $this->assign('yeymoney',$yeymoney);
           $this->assign('allmoney',$allmoney);
           $this->assign('dateS',$dateS);
		   $this->assign('dateE',$dateE);
           $this->assign('name_type','平台');
       		if($type == 2){
		    	$this->display('index_pri');
		    }else{
           		$this->display('index');
		    }
       }else{
           $this->error('您没有权限查看财务报表！');
       }
    }
    //幼儿园进账详情
    public function detail(){
    	$prefix = C('DB_PREFIX');
        $sc_id = I('get.sc_id','','intval');
        $dateS = I('get.dateS');
        $dateE = I('get.dateE');
        $type  = (int)I('get.type')?I('get.type'):1;
        if(empty($sc_id) or empty($dateS) or empty($dateE)){
        	$this->error('参数错误，请重新选择');
        }
        $p= intval($_GET['p'])?$_GET['p']:1;
        $pagesize = 15;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        if($type ==2){
        	$data = M('baoming')
        	->field(" {$prefix}baoming.*,m.uid,u.name as u_name,m.name as m_name,b.body_name as b_name,c.name as c_name ")
        	->join(" {$prefix}user as u on u.id={$prefix}baoming.user_id ","LEFT")
        	->join(" {$prefix}member as m on m.uid={$prefix}baoming.sc_id ","LEFT")
        	->join(" {$prefix}body as b on b.id={$prefix}baoming.body_id ","LEFT")
        	->join(" {$prefix}class as c on c.id={$prefix}baoming.class_id ","LEFT")
        	->where(array('pay_type'=>1,"is_pay"=>1,"{$prefix}baoming.sc_id"=>$sc_id,"{$prefix}baoming.add_time"=>array('between',array($dateS,$dateE))))
        	->order('sc_baoming.add_time desc')
        	->select();
        }else{
        	$data = M('baoming')
        	->field(" {$prefix}baoming.*,m.uid,u.name as u_name,m.name as m_name,b.body_name as b_name,c.name as c_name ")
        	->join(" {$prefix}user as u on u.id={$prefix}baoming.user_id ","LEFT")
        	->join(" {$prefix}member as m on m.uid={$prefix}baoming.sc_id ","LEFT")
        	->join(" {$prefix}body as b on b.id={$prefix}baoming.body_id ","LEFT")
        	->join(" {$prefix}class as c on c.id={$prefix}baoming.class_id ","LEFT")
        	->where(array('pay_type'=>1,"is_pay"=>1,"{$prefix}baoming.sc_id"=>$sc_id,"{$prefix}baoming.add_time"=>array('between',array($dateS,$dateE))))
        	->limit($offset . ',' . $pagesize)
        	->order('sc_baoming.add_time desc')
        	->select();
        }
        $count = M('baoming')
                ->where(array('pay_type'=>1,"is_pay"=>1,"sc_id"=>$sc_id,'add_time'=>array('between',array($dateS,$dateE))))
                ->count();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('dateS',$dateS);
        $this->assign('dateE',$dateE);
        $this->assign('page',$page);
        $this->assign('data',$data);
        if($type == 2){
        	$this->display('detail_pri');
        }else{
        	$this->display();
        }
        
    }
    //获取登录者类别
    public function getType($uid){
        $member = M('member')->where(array('uid'=>$uid))->find();
        return $member['level'];
    }
    //获取平台总收入金额
    public function getAllRecord($dateS,$dateE){
        
        $data = M('baoming')->field("sum(price) as price")->where(array('pay_type'=>1,'is_pay'=>1,'add_time'=>array('between',array($dateS,$dateE))))->select();
        return $data[0]['price'];
    }
    //获取幼儿园总收入金额
    public function getAllYeyRecord($dateS,$dateE,$payType){
    	$where = array('is_pay'=>1,'sc_id'=>$_SESSION['uid'],'add_time'=>array('between',array($dateS,$dateE)));
    	if($payType){
    		$where['pay_type']=$payType;
    	}
        $data = M('baoming')->field("sum(price) as price")->where($where)->select();
        return $data[0]['price'];
    }
    //获取平台每个幼儿园收入金额
    public function getYeyRecord($offset,$pagesize,$dateS,$dateE,$type=1){
    	$prefix = C('DB_PREFIX');
    	if($type == 2){
    		$data = M('baoming')
    		->field("sum(sc_baoming.price) as price,m.name,{$prefix}baoming.sc_id")
    		->join(" {$prefix}member as m on m.uid={$prefix}baoming.sc_id ","LEFT")
    		->group('sc_id')
    		->where(array('pay_type'=>1,'is_pay'=>1,'add_time'=>array('between',array($dateS,$dateE))))
    		->select();
    	}else{
    		$data = M('baoming')
    		->field("sum(sc_baoming.price) as price,m.name,{$prefix}baoming.sc_id")
    		->join(" {$prefix}member as m on m.uid={$prefix}baoming.sc_id ","LEFT")
    		->group('sc_id')
    		->limit($offset . ',' . $pagesize)
    		->where(array('pay_type'=>1,'is_pay'=>1,'add_time'=>array('between',array($dateS,$dateE))))
    		->select();
    	}
        return $data;
    }
}
